/*
 * Copyright (c) 2009 by unreserved.org.
 * All rights reserved.
 */

package org.unreserved.ladderplot;

import org.apache.log4j.Logger;

/**
 * Renders Messages as html.
 */
public class MessageHtmlRenderer implements TextRenderer {
    private static final Logger logger = Logger.getLogger(MessageHtmlRenderer.class);
    
    protected Message msg;
    protected LabelTextRenderer ltr;
    protected int nextMsgId = 1;
    
    public MessageHtmlRenderer(Message msg) {
        this.msg = msg;
        ltr = new LabelTextRenderer(msg.getLabel(), -1);
    }
    
    public int getHeight() {
        return ltr.getHeight() + 1;
    }
    
    public void render() {
        ltr.render();
    }
    
    // OK but not needed
    //public void print(TextCanvas canvas) {
    //    print(canvas, canvas.numLines() - 1);
    //}
    
    private String nextId() {
        return "msg" + (nextMsgId++);
    }
    
    public void print(TextCanvas canvas, int linenum) {
        logger.debug("print: " + linenum);
        /*
        HtmlStringEncoder enc;
        String id = nextId();
        if (msg.hasDetails()) {
            enc = new HtmlStringEncoder(id + "-details");
        } else {
            enc = new HtmlStringEncoder(id);
        }
        canvas.print(linenum, 0, "<
        ltr.print(canvas, linenum, enc);
        
        // print message line
        linenum += ltr.getHeight();
        int x1 = msg.getLeftPlayer().getColumn() + 1;
        int x2 = msg.getRightPlayer().getColumn();
        int lpLen = msg.getLinePattern().length();
        int i = x1;
        while (i < x2) {
            String s = msg.getLinePattern();
            if (x2 - i < lpLen) {
                s = s.substring(0, x2 - i);
            }
            canvas.print(linenum, i, s);
            i += s.length();
        }
        
        // print line ending
        String s = msg.getLineEnding();
        if (s != null && s.length() > 0) {
            if (msg.isLTR()) {
                canvas.print(linenum, x2 - s.length(), s);
            } else {
                canvas.print(linenum, x1, s);
            }
        }
        */
    }
}
